package com.ruoyi.web.controller.project.research;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.project.research.ResearchProjects;
import com.ruoyi.system.domain.project.research.ResearchProjectsMember;
import com.ruoyi.system.service.project.IResearchProjectsMemberService;
import com.ruoyi.system.service.project.IResearchProjectsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 科研项目情况Controller
 * 
 * @author ruoyi
 * @date 2025-08-04
 */
@RestController
@RequestMapping("/project/research")
public class ResearchProjectsController extends BaseController
{
    @Autowired
    private IResearchProjectsService researchProjectsService;
    @Autowired
    private IResearchProjectsMemberService  researchProjectsMemberService;
    /**
     * 查询科研项目情况列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ResearchProjects researchProjects)
    {
        startPage();
        List<ResearchProjects> list = researchProjectsService.selectResearchProjectsList(researchProjects);
        return getDataTable(list);
    }

    /**
     * 导出科研项目情况列表
     */
    @Log(title = "科研项目情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchProjects researchProjects,
                       @RequestParam(required = false) String exportFields)
    {
        List<ResearchProjects> list = researchProjectsService.selectResearchProjectsList(researchProjects);
        ExcelUtil<ResearchProjects> util = new ExcelUtil<ResearchProjects>(ResearchProjects.class);
        // 如果传递了导出字段，则进行处理
        if (StringUtils.isNotBlank(exportFields)) {
            // 将逗号分隔的字段转为数组，调用showColumn指定显示字段
            String[] fields = exportFields.split(",");
            util.showColumn(fields); // 关键：使用ExcelUtil已有的showColumn方法
        }
        util.exportExcel(response, list, "科研项目情况数据");
    }

    /**
     * 获取科研项目情况详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(researchProjectsService.selectResearchProjectsById(id));
    }

    /**
     * 新增科研项目情况
     */
    @Log(title = "科研项目情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchProjects researchProjects)
    {
        ResearchProjectsMember researchProjectsMember = new ResearchProjectsMember();
        if(researchProjects.getId()!=null){
            researchProjectsMember.setClassId(researchProjects.getId());
        }else{
            researchProjectsMember.setClassId(-1L);
        }
        List<ResearchProjectsMember> researchProjectsMembers = researchProjectsMemberService.selectResearchProjectsMemberList(researchProjectsMember);
        Long userId = Long.valueOf(researchProjects.getTeacherId());
        if(researchProjectsMembers!=null&&!researchProjectsMembers.isEmpty()){
            for(ResearchProjectsMember researchProjectsMember1:researchProjectsMembers){
                if(researchProjectsMember1.getMemberId().equals(userId)){
                    return error("此项目您已经参与并有名次记录，请勿重复添加");
                }
            }
        }
        return toAjax(researchProjectsService.insertResearchProjects(researchProjects));
    }

    /**
     * 修改科研项目情况
     */
    @Log(title = "科研项目情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchProjects researchProjects)
    {
        return toAjax(researchProjectsService.updateResearchProjects(researchProjects));
    }

    /**
     * 删除科研项目情况
     */
    @Log(title = "科研项目情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(researchProjectsService.deleteResearchProjectsByIds(ids));
    }

    /**
     * 根据关键字查询
     */
    @GetMapping("/getInfoByTitle")
    public AjaxResult getInfoByTitle(  String data){
        ResearchProjects ra = researchProjectsService.getInfoByTitle(data);
        return success(ra);
    }
}
